﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.BLL;
using CRM.Entity;
using System.Text;
using DP.Common;

namespace CRM.Web
{
    public partial class Message : System.Web.UI.Page, ICallbackEventHandler
    {
        private const string Script1 = "function onCallbackComplete(result){ ClientReply(result); } \n";

        private const string Script2 = "function onCallbackError(){ \n" + " var element = document.getElementById('%ID%'); \n" + " if(element != null) \n" + " element.innerHTML = 'error'; } \n";

        private string CallBackEventTime = String.Empty;

        StringBuilder strResult = new StringBuilder();
        string functionName = "ClientReply";
        UserLoginInfo _userLoginInfo;
        private T_CONFIG_BLL configBll = new T_CONFIG_BLL();
        T_APPOINTMENTREMINDER_BLL appointmentReminderBll = new T_APPOINTMENTREMINDER_BLL();
        T_NOTE_MSG_BLL noteMsgBll = new T_NOTE_MSG_BLL();
        /// <summary>
        /// 
        /// </summary>
        T_REPAIRCASE_BLL repairCaseBll = new T_REPAIRCASE_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_CONSULTINGCASE_BLL consultingCaseBll = new T_CONSULTINGCASE_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_COMPLAINTCASE_BLL complaintCaseBll = new T_COMPLAINTCASE_BLL();
        



        protected void Page_Load(object sender, EventArgs e)
        {
            CallBackEventTime = configBll.GetCacheValueByName(this.Page, "PageCallBackTime");
            if(String.IsNullOrEmpty(CallBackEventTime))
            {
                CallBackEventTime = "60";
            }
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            //LogHelper.WriteLog("Log_CallBackEvent", "Page_Load:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            //LogHelper.WriteLog("Log_CallBackEvent", "Page_Load:" + _userLoginInfo.UserEntity.USERNAME);

        }


        protected override void OnPreRender(EventArgs e)
        {
            // 定义回调字符串。
            string callbackRef = this.Page.ClientScript.GetCallbackEventReference(this, null, "onCallbackComplete", null, "onCallbackError", true);

            // 注册回调脚本块。
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DoClientCallBack", "window.onload = function() { window.setInterval(\"" + callbackRef + "\" ," + CallBackEventTime + "*1000); } \n", true);  //" + CallBackEventTime + "

            // 注册返回脚本块。
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "onCallbackComplete", Script1.Replace("%ID%", this.ClientID), true);
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "onCallbackError", Script2.Replace("%ID%", this.ClientID), true);

            //LogHelper.WriteLog("Log_CallBackEvent", "OnPreRender:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            base.OnPreRender(e);
        }

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            SetReminderMsg(ref strResult);
            SetNoteMsg(ref strResult);

            LogHelper.WriteLog("Log_CallBackEvent", "GetCallbackResult:" + strResult.ToString());
            return strResult.ToString();
            //throw new Exception("The method or operation is not implemented.");
        }

        public void RaiseCallbackEvent(string eventArgument)
        {

            //LogHelper.WriteLog("Log_CallBackEvent", "RaiseCallbackEvent:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        }

        public void SetMarquee(ref StringBuilder Result)
        {
            //strResult = Result + "|" + MarqueeInfo.GetMarqueeInfo(pmAgent);
            //Result = strResult;REMINDER
        }

        /// <summary>
        /// Sets the reminder MSG.
        /// </summary>
        /// <param name="Result">The result.</param>
        public void SetReminderMsg(ref StringBuilder Result)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(String.Format(" and CreatePeopleID = '{0}' ", _userLoginInfo.UserEntity.USERID));
            sql.Append(String.Format(" and Status = '{0}' ", ((int)AppointmentReminderStatus.待提醒).ToString()));
            sql.Append(" and ReminderTime <= sysdate ");

            long count = appointmentReminderBll.GetCount(sql.ToString());      
      
            if (count > 0)
            {
                if (Result.Length != 0)
                {
                    Result.Append("|");
                }
                Result.Append(String.Format("REMINDERMSG*现在有个 {0} 预约业务*AppointmentReminderManage/List.aspx?STATUS=1", count));
            }
        }


        public void SetNoteMsg(ref StringBuilder Result)
        {
            if (Result.Length > 0)
            {
                return;
            }
            StringBuilder sql = new StringBuilder();
            sql.Append(String.Format(" and RecvID = '{0}' ", _userLoginInfo.UserEntity.USERID));
            sql.Append(String.Format(" and Status = '{0}' ", "0"));
            sql.Append(String.Format(" and Recv_BUSINESSDIVISIONCODE = '{0}' ", _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE));

            List<T_NOTE_MSG_Entity> list = noteMsgBll.GetList(sql.ToString(), " AutoID desc ");

            LogHelper.WriteLog("Log_CallBackEvent", String.Format("GetCallbackResult:NoteMsg={0}", list.Count));

            if (list.Count > 0)
            {
                if (Result.Length != 0)
                {
                    Result.Append("|");
                }
                Result.Append(String.Format("NOTEMSG*现在需要处理的内部消息！*MessagingManage/NoteShow.aspx?AUTOID={0}", list.FirstOrDefault().AUTOID));
            }

        }

        public void SetMarqueeMsg(ref StringBuilder Result)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append(String.Format(" and RecvID = '{0}' ", _userLoginInfo.UserEntity.USERID));
            sql.Append(String.Format(" and Status = '{0}' ", "0"));
            sql.Append(String.Format(" and Recv_BUSINESSDIVISIONCODE = '{0}' ", _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE));

            List<T_NOTE_MSG_Entity> list = noteMsgBll.GetList(sql.ToString(), " AutoID desc ");

            LogHelper.WriteLog("Log_CallBackEvent", String.Format("GetCallbackResult:NoteMsg={0}", list.Count));

            if (list.Count > 0)
            {
                if (Result.Length != 0)
                {
                    Result.Append("|");
                }
                Result.Append(String.Format("MARQUEEMSG*现在需要处理的内部消息！*", list.FirstOrDefault().AUTOID));
            }

        }


        #endregion
    }
}
